<template>
  <div class="app-container">
    <el-form size="mini" :inline="true" :model="queryParams" @keyup.enter.native="loadData()">
      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.purchase_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="需求名称">
        <el-input v-model="queryParams.name" placeholder="请输入需求名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="loadData">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="selections.length === 0"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      :data="dataList"
      border
      v-loading="loading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="需求名称"></el-table-column>
      <el-table-column prop="linkman" header-align="center" align="center" label="采购联系人"></el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="联系方式"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.purchase_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column width="200px" fixed="right" header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-notebook-2" size="small" @click="toDetail(scope.row.id)">需求清单
          </el-button>
          <el-button type="text" icon="el-icon-edit" size="small" @click="handleUpdate(scope.row.id)">修改</el-button>
          <el-button type="text" icon="el-icon-delete" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="loadData"
    />

    <purchase-form ref="dataForm" @ok="loadData"></purchase-form>

  </div>
</template>

<script>
import purchaseForm from "./purchaseForm.vue";
import {deletePurchase, purchaseInfoList} from '@/api/ware/purchase'

export default {
  dicts: ['purchase_status'],
  data() {
    return {
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0,
      loading: false,
      selections: [],
    };
  },
  components: {
    purchaseForm
  },
  created() {
    this.loadData();
  },
  methods: {
    resetQuery() {
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
      }
      this.loadData()
    },

    toDetail(id) {
      this.$router.push({
        path: '/ware/purchase-demand',
        query: {
          purchaseId: id
        }
      });
    },

    loadData() {
      this.loading = true;
      purchaseInfoList(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records;
          this.totalPage = res.data.total;
        }
        this.loading = false;
      });
    },
    selectionChangeHandle(val) {
      this.selections = val;
    },
    handleAdd() {
      this.$refs.dataForm.add()
    },
    handleUpdate(id) {
      this.$refs.dataForm.edit(id)
    },

    handleDelete(id) {
      let ids = id ? [id] : this.selections.map(item => {
        return item.id;
      });
      this.$confirm(
        '确定删除选中数据吗',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deletePurchase(ids).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.loadData()
          }
        });
      });
    }
  }
};
</script>
